//login.js
//获取应用实例
var app = getApp();
Page({
    data: {
        remind: '加载中',
        angle: 0,
        userInfo: {},
        regFlag: false // 标识已经注册
    },
    goToIndex:function(){
        wx.switchTab({
            url: '/pages/food/index',
        });
    },
    onLoad:function(){
        wx.setNavigationBarTitle({
            title: app.globalData.shopName
        })
        this.checkLogin()
    },
    onShow:function(){

    },
    onReady: function(){
        var that = this;
        setTimeout(function(){
            that.setData({
                remind: ''
            });
        }, 1000);
        wx.onAccelerometerChange(function(res) {
            var angle = -(res.x*30).toFixed(1);
            if(angle>14){ angle=14; }
            else if(angle<-14){ angle=-14; }
            if(that.data.angle !== angle){
                that.setData({
                    angle: angle
                });
            }
        });
    },
    checkLogin: function () {
        const that = this;
        wx.login({
            success: function (res) {
                if (!res.code) {
                    app.alert({'content': '登录失败, 请再次点击~' });
                    return;
                }
                wx.request({
                    url: app.buildUrl('/member/check-reg'),
                    method: 'POST',
                    data: {
                        code: res.code
                    },
                    header: app.getRequestHeader(),
                    success: function (res) {
                        if (res.data.code === 200) {
                            let token = res.data.data.token
                            app.setCache("token", token)
                            that.goToIndex()
                        } else {
                            that.setData({
                                regFlag: false
                            })
                            return;
                        }
                    }
                })
            }
        })
    },
    getUserProfile: function () {
        let data = {}
        const that = this;
        wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {
                data = res.userInfo
                wx.login({
                    success: function (res) {
                        if (!res.code) {
                            app.alert({'content': '登录失败, 请再次点击~' });
                            return;
                        }
                        wx.request({
                            url: app.buildUrl('/member/login'),
                            method: 'POST',
                            data: {
                                ...data,
                                code: res.code
                            },
                            header: app.getRequestHeader(),
                            success: function (res) {
                                if (res.data.code !== 200) {
                                    app.alert({'content': res.data.msg});
                                    return;
                                }
                                let token = res.data.data.token
                                app.setCache("token", token)
                                that.goToIndex()
                            }
                        })
                    }
                })
            },
            fail: (err) => {
                app.alert({'content': '登录失败, 请再次点击~' + err});
            }
        })
    }
});